package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import com.kaishengit.entity.Document;
import com.kaishengit.entity.Project;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class DocumentDao {
	private DBHelp<Document> db=new DBHelp<Document>();
	public long findCount(String id){
		String sql = "select count(*) from t_document where t_project_id =?";
		return (Long) db.executeForFunction(sql,id);
	}
	
	public boolean save(Document document){
		String sql="INSERT INTO t_document(id,title,content,createtime,t_user_id,t_project_id) VALUES (?,?,?,?,?,?)";
		return db.executeSQL(sql,document.getId(),document.getTitle(), document.getContent(),document.getCreatetime(),document.getUserid(),document.getProjectid());
	}
	public Document findbyId(String id){
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT td.id,title,content,td.createtime,t_user_id,t_project_id,tu.username,tu.password,tu.imgname,tp.name,tp.description,tp.createtime as tpcreatetime FROM t_document AS td  ");
		sql.append("LEFT JOIN t_user AS tu ON tu.id = td.t_user_id ");
		sql.append("LEFT JOIN t_project AS tp ON tp.id = td.t_project_id ");
		sql.append("WHERE td.id = ?");
		return db.executeQueryForObject(sql.toString(), new DocumentRowMapper(),id);
	}
/*	public List<Document> findAll(){
		String sql="SELECT id,title,content,createtime,userid FROM t_document ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new DocumentRowMapper());
	}*/
	public List<Document> findAllByProjectId( int offest, int pagesize,String projectid){
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT td.id,title,content,td.createtime,t_user_id,t_project_id,tu.username,tu.password,tu.imgname,tp.name,tp.description,tp.createtime as tpcreatetime FROM t_document AS td  ");
		sql.append("LEFT JOIN t_user AS tu ON tu.id = td.t_user_id ");
		sql.append("LEFT JOIN t_project AS tp ON tp.id = td.t_project_id ");
		sql.append("WHERE td.t_project_id = ? order by td.createtime desc  limit ?,?");
		return db.executeQueryForList(sql.toString(), new DocumentRowMapper(),projectid,offest,pagesize);	
	}
	
	public class DocumentRowMapper implements RowMapper<Document>{

		public Document mapRow(ResultSet rs) throws SQLException {
			Document document=new Document();
			document.setId(rs.getString("id"));
			document.setContent(rs.getString("content"));
			document.setCreatetime(rs.getString("createtime"));
			document.setTitle(rs.getString("title"));
			document.setUserid(rs.getString("t_user_id"));
			try {
				document.setResult(rs.getString("createtime"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			User user = new User();
			user.setUsername(rs.getString("username"));
			user.setPassword(rs.getString("password"));
			user.setImgname(rs.getString("imgname"));
			user.setId(rs.getString("t_user_id"));
			document.setUser(user);
			
			Project p = new Project();
			p.setCreatetime(rs.getString("tpcreatetime"));
			p.setDescription(rs.getString("description"));
			p.setId(rs.getString("t_project_id"));
			p.setName(rs.getString("name"));
			document.setProject(p);
			
			
			return document;
		}
		
	}

	public void update(String id, String title, String content, String time) {
		String sql = "update t_document set title = ?,content=?,createtime=? where id = ?";
		db.executeSQL(sql, title,content,time,id);
		
	}

	public void del(String id) {
		String sql = "delete from t_document where id = ?";
		db.executeSQL(sql, id);
		
	}
}
